package com.suray.wcs.service.init.device;

import com.suray.basic.wcs.path.core.Node;
import com.suray.basic.wcs.plc.pojo.device.Scanner;
import com.suray.basic.wcs.utils.Coord;
import com.suray.wcs.service.system.entity.DeviceDB;

import java.util.ArrayList;
import java.util.List;

public class ScanManager {
    public final List<Scanner> scanners = new ArrayList<>();

    public ScanManager() {

    }

    public void createScan(DeviceDB deviceDB) {
        scanners.add(new Scanner(deviceDB.getName(), deviceDB.getDeviceNo(), deviceDB.getIp(), deviceDB.getPort(),
                deviceDB.getLocation() == null ? null : Node.parseGrid(deviceDB.getLocation()), !deviceDB.isDisable()));
    }

    /**
     * 根据设备编号查找设备
     * @param deviceNo
     * @return
     */
    public Scanner getScanByNo(String deviceNo) {
        for (Scanner scanner : scanners) {
            if (scanner.getDeviceNo().equals(deviceNo)) {
                return scanner;
            }
        }

        return null;
    }

    /**
     * 根据设备编号查找设备
     * @param name
     * @return
     */
    public Scanner getScanByName(String name) {
        for (Scanner scanner : scanners) {
            if (scanner.getScanName().equals(name)) {
                return scanner;
            }
        }

        return null;
    }

    /**
     * 根据设备编号查找设备
     * @param coord
     * @return
     */
    public Scanner getScanByCoord(Coord coord) {
        for (Scanner scanner : scanners) {
            if (coord.equals(scanner.getLocation())) {
                return scanner;
            }
        }

        return null;
    }
}
